package com.titan.core.utils.dbinfo.dialect.impl;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.titan.core.utils.data.StringUtils;
import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.dbinfo.dialect.IDBDialect;
import com.titan.core.utils.file.ClassUtils;
import com.titan.core.utils.object.TableColumnDescriptor;
import com.titan.core.utils.object.TableDescriptor;

/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月14日
 */
public class MSSQLDialect implements IDBDialect {
    private final static String DRIVER_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    static {
        try {
            if(ClassUtils.isExistClass(DRIVER_NAME)) {
                Driver driver = (Driver) Class.forName(DRIVER_NAME).newInstance();
                DriverManager.registerDriver(driver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    @Override
    public String getDriverName() {
        return DRIVER_NAME;
    }

    @Override
    public String getUrl(String ip, int port, String serviceName) {
        return StringUtils.append("jdbc:sqlserver://", ip, ":", port, ";DatabaseName=", serviceName);
    }

    @Override
    public Connection getConnection(String ip, int port, String serviceName, String userid, String password) throws SQLException {
        String url = getUrl(ip, port, serviceName);
        return getConnection(url, userid, password);
    }

    @Override
    public Connection getConnection(String url, String userid, String password) throws SQLException {
        return DriverManager.getConnection(url, userid, password);
    }

    @Override
    public String identifier(String key) {
        return new StringBuilder(key.length()+2).append('[').append(key).append(']').toString();
    }

    @Override
    public String sqlSysdate() {
        return "now()";
    }

    @Override
    public void sqlPage(StringBuilder sql, int pageIndex, int pageSize) {
        throw new RuntimeException("未定义！");
    }

    @Override
    public List<String> getTables(Connection conn, String schema) throws Exception {
        String sql = "select name tname from sys.tables";
        List<String> result =new ArrayList<String>();
        Statement stmt = null;
        ResultSet rest = null;
        try {
            stmt = conn.createStatement();
            rest = stmt.executeQuery(sql);
            while(rest.next()) {
                String name = rest.getString(1);
                result.add(name);
            }
            return result;
        } finally {
            DBUtils.close(rest);
            DBUtils.close(stmt);
        }
    }
    
    @Override
    public TableColumnDescriptor[] getColumn(Connection conn, String schema, String tablename) throws Exception {
        throw new RuntimeException("未定义！");
    }

    @Override
    public TableDescriptor getTable(Connection conn, String schema, String tablename) throws Exception {
        throw new RuntimeException("未定义！");
    }

    @Override
    public String[] getPrimaryKey(Connection conn, String schema, String tablename) throws Exception {
        throw new RuntimeException("未定义！");
    }

    @Override
    public String[][] getUniqueIndex(Connection conn, String schema, String tablename) throws Exception {
        return null;
    }
}
